import {IMock} from './i-mock';
import {MockConfigItem} from '../../entity/mock-config-item';
import {MockUtils} from '../mock-utils';
import {AlertMessage} from '../../entity/alert-message';

export class CollectionMock implements IMock {
    getBaseTypeMockConfigDisplay(mockConfigItem: MockConfigItem): string {
        const loopCount = mockConfigItem.mockValueStart;
        switch (mockConfigItem.mockType) {
            case 0:
                // 固定循环
                if (loopCount > 1 && (mockConfigItem.children.length === 1 || !mockConfigItem.mockListLoopStructCreated)) {
                    // 未生成
                    return '// @loop(' + loopCount + ')';
                } else {
                    return '';
                }
            case 1:
                return '// @loop(' + loopCount + ', ' + mockConfigItem.mockValueEnd + ')';

        }
        return '';
    }

    onSave(mockConfigItem: MockConfigItem): boolean {
        if (mockConfigItem.mockType === 0) {
            // 固定循环次数
            const result = MockUtils.checkNumber(mockConfigItem.mockValueStart, 0, 100, '固定循环次数');
            if (!result) {
                return false;
            }
        }
        if (mockConfigItem.mockType === 1) {
            // 随机循环次数
            let result = MockUtils.checkNumber(mockConfigItem.mockValueStart, 0, 100, '随机循环次数，起始值');
            if (!result) {
                return false;
            }

            result = MockUtils.checkNumber(mockConfigItem.mockValueEnd, 1, 100, '随机循环次数，结束值');
            if (!result) {
                return false;
            }

            if (mockConfigItem.mockValueStart > mockConfigItem.mockValueEnd) {
                MockUtils.setMockInfo(AlertMessage.error('随机循环次数，起始值不能大于结束值！'));
                return false;
            }
        }
        return true;
    }

}
